package com.le.tester.javaThink.fourth.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * createTime：2021/9/17 16:44
 * description：AtomicityTest，加上的锁，依旧还是不行的
 *
 */
public class AtomicityTest implements Runnable {

    private int i = 0;

    public int getValue() {
        return i;
    }

    //加了synchronized,两次++
    private synchronized void evenIncrement() {
        i++;
        i++;
    }

    @Override
    public void run() {
        while (true)
            evenIncrement();

    }

    public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();
        AtomicityTest at = new AtomicityTest();
        service.execute(at);
        while (true) {
            int value = at.getValue();
            //是不是偶数
            if (value % 2 != 0) {
                System.out.println(value);
                System.exit(0);
            }
        }
    }
}
